<?php

namespace App\Console\Commands;

use App\Services\Account\AccountService;
use App\Services\Customer\CustomerService;
use App\Services\Goods\CommentService;
use App\Services\Goods\GoodsService;
use App\Services\Report\DashboardService;
use App\Services\Sales\OrderService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

/**
 * 运营端首页经营概况数据统计
 *
 * @author WangBin
 * @date 2022-01-13
 * @package App\Console\Commands
 */
class DashboardStatCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'report:dashboard';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '运营端首页经营概况数据统计';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 同步生活杉德订单支付状态
     *
     * @return int
     */
    public function handle()
    {
        // 统计 用户数、商品数、订单数、评论数、客户余额
        Log::debug('[任务调度][运营端首页经营概况数据统计] 正在执行...');
        // 新建报表统计记录
        $reportId = DashboardService::getInstance()->create();
        if(empty($reportId)) {
            Log::error("[任务调度][运营端首页经营概况数据统计] 创建报表统计记录失败！");
            exit();
        }
        // 统计数据
        $data = [];

        // 统计平台注册用户数
        $customerCount = CustomerService::getInstance()->getCount();
        $data['customer_count'] = $customerCount;

        // 统计平台商品数
        $goodsCount = GoodsService::getInstance()->getCount();
        $data['goods_count'] = $goodsCount;

        // 统计平台订单数（排除：已取消）
        $orderCount = OrderService::getInstance()->getCount();
        $data['order_count'] = $orderCount;

        // 统计平台评论数（审核通过）
        $commentCount = CommentService::getInstance()->getCount();
        $data['comment_count'] = $commentCount;

        // 统计客户账户余额
        $customerBalance = AccountService::getInstance()->getCustomerBalanceTotal();
        $data['balance'] = $customerBalance;

        // 保存统计数据
        $reportId = DashboardService::getInstance()->update(['data' => $data, 'status' => 2], $reportId);
        if(empty($reportId)) {
            Log::error("[任务调度][运营端首页经营概况数据统计] 更新报表统计记录失败！");
            exit();
        }
        Log::debug('[任务调度][运营端首页经营概况数据统计] 完成执行...');
    }
}
